/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bda.mediatheque.controller;

import com.bda.mediatheque.entities.Exemplaire;
import com.bda.mediatheque.entities.Item;
import com.bda.mediatheque.entities.Lignedepanier;
import com.bda.mediatheque.entities.Panier;
import com.bda.mediatheque.entities.Utilisateur;
import com.bda.mediatheque.service.IItemDao;
import com.bda.mediatheque.service.ILigneDePanierDao;
import com.bda.mediatheque.service.IPanierDao;
import com.bda.mediatheque.service.IUtilisateurDao;
import com.bda.mediatheque.service.ImpItemDao;
import com.bda.mediatheque.service.ImplLigneDePanierDao;
import com.bda.mediatheque.service.ImplPanierDao;
import com.bda.mediatheque.service.ImplUtilisateurDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author samsung
 */
@WebServlet(name = "ValidateUserPanierServlet", urlPatterns = {"/ValidateUserPanier"})
public class ValidateUserPanierServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        String forwardStatement = "SearchItemServlet";
        try {
            //Get the id's of the selected items
            int numberOfItem  = Integer.parseInt((String) request.getParameter("numberOfItem"));
            int[] items = new int[numberOfItem];
            for (int index = 0; index < numberOfItem;index++)
            {
                items[index]  = Integer.parseInt((String) request.getParameter("item"+index));
            }
            String loggedUser = (String)request.getSession().getAttribute("loggedUser");
            IUtilisateurDao utilisateurDao = new ImplUtilisateurDao();
            IItemDao itemDao = new ImpItemDao();
            IPanierDao panierDao = new ImplPanierDao();
            Utilisateur user = utilisateurDao.findUserByLogin(loggedUser);
            if (null != user)
            {
                 // construct a list of exemplaire
                 List<Exemplaire> recommendedExemplaire = new  ArrayList();
                 List<Item> toReserveItem = new ArrayList();
                 for (int index = 0; index < numberOfItem;index++)
                 {
                     Item item = itemDao.findById(items[index]);
                     List exemplaires = item.getExemplaireList();
                     Iterator iterator = exemplaires.iterator();
                     boolean toReserve = true;
                     while (iterator.hasNext())
                     {
                         Exemplaire exemplaire = (Exemplaire) iterator.next();
                         if(exemplaire.getPresent())
                         {
                             recommendedExemplaire.add(exemplaire);
                             toReserve = false;
                             break;
                         }
                     }
                     if (toReserve)
                     {
                         toReserveItem.add(item);
                     }
                 }
                 Panier panier = user.getPanier();
                 Iterator exempIter = recommendedExemplaire.iterator();
                 ILigneDePanierDao ligneDePanierDao = new ImplLigneDePanierDao();
                 while (exempIter.hasNext())
                 {
                    Exemplaire exemplaire = (Exemplaire) exempIter.next();
                    Lignedepanier ligneDePanier = new Lignedepanier();
                    ligneDePanier.setPanier(panier);
                    ligneDePanier.setExemplaire(exemplaire);
                    exemplaire.setPresent(false);
                    ligneDePanierDao.merge(ligneDePanier);
                 }
                 int validetedPanier = 1;
                 panier.setEtat(validetedPanier);
                 panierDao.merge(panier);
                 request.getRequestDispatcher(forwardStatement).forward(request,response);
            }
        } catch (Exception exp) {           
            exp.printStackTrace();
            request.getRequestDispatcher(forwardStatement).forward(request,response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
